package com.gali.lovefinder.utils;

import com.gali.lovefinder.common.ErrorCode;
import com.gali.lovefinder.exception.BusinessException;
import org.springframework.util.CollectionUtils;

import java.util.Collection;

/**
 * @Auther: gali
 * @Date: 2023-03-05 19:27
 * @Description:参数校验类
 */
public class AssertUtil {

    @FunctionalInterface
    public interface Customer {
        void customer();
    }

    /**
     * 判断参数是否未空
     */
    public static <T> void isNull(T param, ErrorCode errorCode, Customer customer) {
        if (param == null) {
            customer.customer();
            throw new BusinessException(errorCode);
        }
    }

    /**
     * 判断参数是否未空
     */
    public static <T> void isNull(T param, ErrorCode errorCode) {
        if (param == null) {
            throw new BusinessException(errorCode);
        }
    }

    /**
     * 判断参数是否正确
     */
    public static void isTrue(boolean param, ErrorCode errorCode, Customer customer) {
        if (!param) {
            customer.customer();
            throw new BusinessException(errorCode);
        }
    }

    /**
     * 判断参数是否正确
     */
    public static void isTrue(boolean param, ErrorCode errorCode) {
        if (!param) {
            throw new BusinessException(errorCode);
        }
    }


    /**
     * 判断集合是否为空
     */
    public static <T> void isCollect(Collection<T> collection, ErrorCode errorCode, Customer customer) {
        if (CollectionUtils.isEmpty(collection)) {
            customer.customer();
            throw new BusinessException(errorCode);
        }
    }

    /**
     * 判断集合是否为空
     */
    public static <T> void isCollect(Collection<T> collection, ErrorCode errorCode) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BusinessException(errorCode);
        }
    }
}
